﻿namespace Web.ProcessScreenCapture
{
    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;
    using System.IO;

    public class ImageParams
    {
        private EncoderParameters _encoder;
        private string _filePath;
        private ImageCodecInfo _ici;
        private long _quality;
        private static Dictionary<string, string> _typeDic = new Dictionary<string, string>();

        internal ImageParams() : this(string.Empty)
        {
        }

        internal ImageParams(string filePath)
        {
            if (_typeDic.Count == 0)
            {
                _typeDic.Add(".bmp", "bmp");
                _typeDic.Add(".jpg", "jpeg");
                _typeDic.Add(".jpeg", "jpeg");
                _typeDic.Add(".png", "png");
                _typeDic.Add(".tif", "tiff");
                _typeDic.Add(".tiff", "tiff");
            }
            this._quality = 100L;
            this._filePath = string.Empty;
            this._encoder = null;
            this._ici = null;
            this.AnalyzePath(filePath);
        }

        protected void AnalyzePath(string filePath)
        {
            filePath = filePath.ToLower();
            if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(Path.GetFileNameWithoutExtension(filePath)))
            {
                throw new Exception("Invalid File Path!");
            }
            try
            {
                string str2;
                string extension = Path.GetExtension(filePath);
                if (string.IsNullOrEmpty(extension))
                {
                    extension = ".jpeg";
                }
                if (_typeDic.TryGetValue(extension, out str2))
                {
                    string mimeType = "image/" + str2;
                    this._ici = this.GetEncoderInfo(mimeType);
                }
                if (string.IsNullOrEmpty(Path.GetDirectoryName(filePath)))
                {
                    this._filePath = Directory.GetCurrentDirectory() + @"\" + filePath;
                }
                this._encoder = new EncoderParameters(1);
                this._encoder.Param[0] = new EncoderParameter(Encoder.Quality, this._quality);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                throw new Exception("Error Ocurred when analyzing the file path.", exception);
            }
        }

        protected ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo info in imageEncoders)
            {
                if (info.MimeType == mimeType)
                {
                    return info;
                }
            }
            return null;
        }

        internal ImageCodecInfo CodeInfo
        {
            get
            {
                return this._ici;
            }
        }

        internal EncoderParameters EncoderParam
        {
            get
            {
                return this._encoder;
            }
        }

        internal string FilePath
        {
            get
            {
                return this._filePath;
            }
            set
            {
                this._filePath = value;
                this.AnalyzePath(this._filePath);
            }
        }

        internal long Quality
        {
            set
            {
                this._quality = value;
            }
        }
    }
}

